<?php /** @var User $thatUser */ ?>
<?php /** @var User $CurrentUser */ ?>
<div id="main">
    <div id="user-center-sidebar-left">
        <?php $this->load->view('home/user-center/sidebar'); ?>
    </div>
    <div id="user-center-body">
        <?php if (isset($CommodityArray)):; ?>
            <?php foreach ($CommodityArray as $thisCommodity): ?>
                <?php /** @var Commodity $thisCommodity */ ?>

                <div class="commodity">
                    <div class="commodity-image-block">
                        <?php if ($thisCommodity->getStock() <= 0) : ?>
                            <img src="<?php echo base_url('public/images/sell-out.png') ?>" alt="已售完" style="position: absolute;top: 0;right: 0;">
                        <?php endif; ?>
                        <a href="<?php echo base_url('home/details/' . $thisCommodity->getId()) ?>"><img src="<?php echo $thisCommodity->getDisplayImageUrl() ?>" alt="商品图片"></a>
                    </div>
                    <div class="commodity-content-block">
                        <div class="commodity-title-block">
                            <span class="text-primary commodity-name"><a href="<?php echo base_url('home/details/' . $thisCommodity->getId()) ?>"><?php echo $thisCommodity->getTitle() ?></a></span>

                            <span class="pull-right text-info commodity-time"><?php echo $thisCommodity->getReleaseTime() ?></span>
                        </div>
                        <div class="commodity-intro">
                            <?php echo $thisCommodity->getIntro() ?>
                        </div>
                    </div>
                    <div class="clearfix"></div>

                    <div class="user-commodity-footer">
                        <?php if ($thatUser->getId() == $CurrentUser->getId()): ?>
                            <div class="pull-left">
                                <?php if ($thisCommodity->getStock() >= 0): ?>
                                    <a href="javascript:" class="edit-commodity" data-commodity-id="<?php echo $thisCommodity->getId() ?>"><span class="glyphicon glyphicon-edit"></span>&nbsp;编辑</a>
                                <?php endif; ?>
                                <?php if ($thisCommodity->getStock() > 0): ?>
                                    <a href="javascript:" class="put-top-commodity" data-commodity-id="<?php echo $thisCommodity->getId() ?>"><span class="glyphicon glyphicon-pushpin"></span>&nbsp;置顶</a>
                                <?php endif; ?>
                                <a href="javascript:" class="delete-commodity" data-commodity-id="<?php echo $thisCommodity->getId() ?>"><span class="glyphicon glyphicon-trash"></span>&nbsp;删除</a>
                                <?php if ($thisCommodity->getStock() != 0): ?>
                                    <a href="javascript:" class="sell-out" data-commodity-id="<?php echo $thisCommodity->getId() ?>"><span class="glyphicon glyphicon-flag"></span>&nbsp;售完</a>
                                <?php endif; ?>
                            </div>
                        <?php else: ?>
                            <a href="javascript:" class="commodity-toggle-favorite" data-commodity-id="<?php echo $thisCommodity->getId() ?>"><span class="glyphicon glyphicon-heart"></span>&nbsp;收藏</a>
                            <a href="javascript:" class="commodity-toggle-plus-one" data-commodity-id="<?php echo $thisCommodity->getId() ?>"><span class="glyphicon glyphicon-thumbs-up"></span>&nbsp;赞</a>
                        <?php endif; ?>
                        <div class="pull-right">
                            <span style="padding: 8px 15px;font-size: 12px;color: #828282; display:inline-block;">售价：<?php echo $thisCommodity->getDisplayPrice() ?></span>
                        </div>
                    </div>

                </div>
            <?php endforeach; ?>
        <?php endif; ?>
        <div id="pagination">
            <?php if (isset($Pagination)) echo $Pagination; ?>
        </div>
    </div>
</div>

<script type="text/javascript">
$(document).on('click', '.commodity-toggle-plus-one', function () {
	var _this = this;
	var commodityId = $(this).data('commodity-id');
	$.ajax({
		url: '<?php echo base_url('api/commodityTogglePlusOne');?>',
		type: 'get',
		data: {commodity_id: commodityId},
		beforeSend: function () {
		},
		complete: function () {
		},
		success: function (jsonrsp) {
			try {
				var rsp = $.parseJSON(jsonrsp);
				switch (rsp.type) {
					case 'success':
						if (rsp.data.isCommodityPlusOneNow) {
							$(_this).html('<span class=" glyphicon glyphicon-thumbs-up"></span>已赞(' + rsp.data.countCommodityPlusOneNow + ')');
						} else {
							$(_this).html('<span class=" glyphicon glyphicon-thumbs-up"></span>赞(' + rsp.data.countCommodityPlusOneNow + ')');
						}
						break;
					case 'error':
						if (rsp.message == 'NOTLOGIN') {
							showDialog('login');
						}
						break;
					default:
						break;
				}
			} catch (e) {
				console.error(e);
				noty({type: 'error', text: '发生内部错误，请联系管理员'});
			}
		}
	});
});

$(document).on('click', '.commodity-toggle-favorite', function () {
	var _this = this;
	var commodityId = $(this).data('commodity-id');
	$.ajax({
		url: '<?php echo base_url('api/commodityToggleFavorite');?>',
		type: 'get',
		data: {commodity_id: commodityId},
		beforeSend: function () {
		},
		complete: function () {
		},
		success: function (jsonrsp) {
			try {
				var rsp = $.parseJSON(jsonrsp);
				switch (rsp.type) {
					case 'success':
						if (rsp.data.isCommodityInFavoriteNow) {
							$(_this).html('<span class="glyphicon glyphicon-heart"></span>已收藏');
						} else {
							$(_this).html('<span class="glyphicon glyphicon-heart"></span>收藏');
						}
						break;
					case 'error':
						if (rsp.message == 'NOTLOGIN') {
							showDialog('login');
						}
						break;
					default:
						break;
				}
			} catch (e) {
				console.error(e);
				noty({type: 'error', text: '发生内部错误，请联系管理员'});
			}
		}
	});
});
</script>